<?php include 'header.php'; ?>
<?php
//이메일 주소 정보를 받아와서 registerForm의 email값에 세팅
require "facebook/facebook.php";
$fb = new Facebook(array(
	"appId"=>"115502678537142",
	"secret"=>"24b002eb0b0b09332bbf635972988504",
        "cookie"=>true
));

$fbUserID = $fb->getUser();
//var_dump($fbUserID);
if($fbUserID) {
$userInfo = $fb->api("/me");
$userEmail = $userInfo["email"];
//echo $userEmail;
}
?>
<script type="text/javascript">
		//<!--
		var latitude;
		var longitude;
		var req;
		
		$(function()
			{
				var o = new Object();
				o.req = "GET_PERFORMANCE_DETAIL";
				req = o.req;
	<?php
			echo "o.performance_seq =" . $_REQUEST["performance_seq"] . ";\n";
	?>	
				var url = "http://gilstar.com/gilstar/Performance.jsp";
				getJSON(url, o);			
			});

	function getJSON(url, json)
	{
		var requestStr = JSON.stringify(json, replacer);
		
		$.ajax(
			{
				url: url,
				data: requestStr,
				type: "POST",
				success: function(data)
				{
					var resultObject = jQuery.parseJSON(data);
					jsonResponseHandler(resultObject);
				}
			});
	}

	function jsonResponseHandler(result)
	{
		//alert(req);
		if(result.ResultCode == -1)			//처리 도중 실패.
		{
			alert(result.ResultMessage);
		}
		else if(result.ResultCode == 0)	//성공적으로 처리 완료.
		{
			
			if(req == "DELETE_PERFORMANCE")
			{
				moveTab("map.php");
				return;
			}
			var item = result.ResultMethod[0];
			
			//check time
			var d = new Date();
			var performanceDate = stringToDate(item.time);
			
			var imagePath = item.file_name;

			if(imagePath != "")
			{
				$(".listThumb").attr({src : imagePath});
			}
			
			$(".listH").html(item.title);
			$(".listType").html(item.category);
			$(".listArtist").html(item.artist);
			$(".listTime").html("<time datetime>" + item.time + "</time>");
			$(".listLoc").html("<a href='#' onclick='showMap();'>" + item.address + "</a>");

			if(item.content == "")
			{
				$(".introduce_p").text("등록된 소개글이 없습니다.");
			}
			else
			{
				$(".introduce_p").text(item.content);
			}
			
			if(item.youtube_link == "http://")
			{
				$("#youtube_link").html("등록된 동영상이 없습니다.");
			}
			else
			{
				$("#youtube_link").html("<a href=\"" + item.youtube_link + "\">" + item.youtube_link + "</a>");
			}
			
			$("#sns_url").attr({href : item.sns_url}).html(item.sns_url);

			latitude = item.latitude;
			longitude = item.longitude;
			
			if(performanceDate.valueOf() - d.valueOf() > 0)
			{
				$('.viewEmpty').hide();
				$('.view').show();				 
			}
			else
			{
				$('.viewEmpty').show();
				$('.view').hide();
			}
			//alert(item.email);
			<?php
				if(empty($userEmail) != true)
				{
					echo "if(\"" . $userEmail ."\"". " == item.email){\n";
					echo "showDelButton();\n";
					echo "}\n";
				}
			?>	
			showMap();
		}
	}

	function showDelButton()
	{
		$("#btnDel").css("display", "inline-block");
	}

	function stringToDate(str)
	{
		
		var yearStr = str.substring(0, 4);
		var monthStr = str.substring(5, 7);
		var dayStr = str.substring(8, 10);

		var hourStr = str.substring(11, 13);
		var minuteStr = str.substring(14, 16);

		var nYear = parseInt(yearStr, 10);
		var nMonth = parseInt(monthStr, 10);
		var nDay = parseInt(dayStr, 10);
		var nHour = parseInt(hourStr, 10);
		var nMinute = parseInt(minuteStr, 10);

		var d = new Date(nYear, (nMonth - 1), nDay, (nHour + 1), nMinute);
		
		return d;
	}

	function showMap()
	{
		var currentLocation = new google.maps.LatLng(latitude, longitude);
		//alert(currentLocation);
		var myOptions = { zoom: 15, center: currentLocation, mapTypeId: google.maps.MapTypeId.ROADMAP };
		var map = new google.maps.Map(document.getElementById("mapView"), myOptions);			

		var marker = new google.maps.Marker(
			{
					position : currentLocation,
					title : "my location",
					map : map
			});
	}

	function replacer(key, value) 
	{
	    if (typeof value === 'number' && !isFinite(value)) 
	    {
	        return String(value);
	    }
	    return value;
	}

	function sendFacebook()
	{
		<?php
			echo "location.href=\"http://www.facebook.com/share.php?u=http://gilstar.com/main/view.php?performance_seq=" . $_REQUEST["performance_seq"] . "\";"
		?>
	}

	function sendTwitter()
	{
		<?php
			echo "location.href=\"http://twitter.com/share?url=http://gilstar.com/main/view.php?performance_seq=" . $_REQUEST["performance_seq"] . "\";"
		?>	
		
	}

	function copyClipBoard()
	{
		<?php
			echo " var str = \"http://gilstar.com/main/view.php?performance_seq=" . $_REQUEST["performance_seq"] . "\";";
		?>
		copyToClipboard(str);
	}

	function copyToClipboard(text)
	{
		$.copy(text);
	    alert("클립보드로 복사되었습니다.");
	}

	function handleDelete()
	{
		var o = new Object();
		o.req = "DELETE_PERFORMANCE";
		<?php 
			echo "o.performance_seq = ".$_REQUEST["performance_seq"]. ";";
		?>
		
		req = o.req;
		
		var url = "http://gilstar.com/gilstar/Performance.jsp";
		
		getJSON(url, o); 
	}
//-->
</script>

<!-- 상세 보기 -->
<section id="view" class="view"> 

	<div class="defaultInfo">
		<div class="listThumbWrap">
			<!-- [D] 이미지를 등록하지 않은 경우 아래 이미지를 출력하지 않으시면 됩니다. -->
			<img width="100" height="100" class="listThumb"> 
		</div>
		<div class="listContent"> 
			<h1 class="listH"></h1>
			<ul>
			<li class="listArtist"></li>
			<li class="listType"></li> 
			<li class="listTime">
			</li>
			<li class="listLoc"></li>
			</ul> 
		</div>
	</div>
	<div class="viewMapArea">
		<div class="viewMap" id="mapView">
		</div> 
	</div>
	
	<div class="introduce">
		<h2 class="introduce_h">소개글 <span class="introduce_h_desc">Introduce</span></h2>
		<p class="introduce_p" id="content">
		</p>
	</div>
	
	<div class="otherInfo">
		<h2 class="hidden">추가 정보</h2>
		<ul class="otherInfoUl">
		<li class="otherInfoLi">
			<span class="icon">동영상:</span>
			<span class="otherInfoA" id="youtube_link"></span>
			<!--
			<a href="#" class="otherInfoA">http://youtu.be/tCVkHAuGBAQ</a>
			--> 
		</li>
		<li class="otherInfoLi">
			<span class="icon">SNS:</span>
			<a class="otherInfoA" id="sns_url"></a>
		</li>
		</ul>
	</div>

	<h2 class="hidden">친구들과 공유하기</h2>
	<div class="share">
		<span class="shareCell shareBtnFacebook">
			<button type="button" class="shareBtn" onclick="sendFacebook();">facebook</button>
		</span>
		<span class="shareCell shareBtnTwitter">
			<button type="button" class="shareBtn" onclick="sendTwitter();">twitter</button>
		</span>
		<span class="shareCell shareBlank"></span>
		<span class="shareCell shareBtnCopy">
			<button type="button" class="shareBtn" onclick="copyClipBoard();">주소복사</button>
		</span>
	</div>
	
	<div class="btnViewWrap">
		<button type="button" class="btnDel" onclick="handleDelete();" id="btnDel" style="display: none">삭제</button>
		<a href="list.php" class="btnList">목록보기</a>
	</div>

</section> 

<!-- [D] 공연이 없을 때 -->
<section class="viewEmpty">
	<p class="viewEmptyVisual">안녕! 다음에 또 만나요!</p>
	<h1 class="viewEmptyH">이미 끝난 공연입니다.</h1>
	<p class="viewEmptyMessage">오늘, 내 주변에서 일어나는 공연은?</p>
	<p class="viewEmptyGo"><a href="map.php" class="viewEmptyBtn">공연 보러가기</a></p>
</section>
<!-- //상세 보기 --> 

<?php include 'footer.php'; ?>